package com.tonghe.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Date:2021/12/18- 12- 18- 13:13
 **/
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;//提升作用域

    static{
        try {
            //使用Mybatis的第一步，获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //可以从sqlSession中获取实例了，
    //sqlSession 完全包含了面向数据库执行sql命令所需的素有方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
